document.getElementById("loginForm").addEventListener("submit", async function (e) {
    e.preventDefault(); // 阻止默认提交行为

    const username = document.getElementById("username").value.trim();
    const password = document.getElementById("password").value;
    const message = document.getElementById("message");

    if (!username || !password) {
        message.textContent = "用户名和密码不能为空！";
        return;
    }

    try {
        // 动态导入 HttpUtil 模块
        const { Http } = await import('../utils/HttpUtil.js'); // 注意路径是否正确

        // 发送登录请求
        const res = await Http.post('/sys/login/backendLogin', {
            username: username,
            password: password
        });

        console.log(res);

        if (res.code === 200) {
            message.style.color = "green";
            message.textContent = "登录成功！跳转中...";
            setTimeout(() => {
                // 跳转到首页
                window.location.href = "../dashboard/dashboard.html";
                localStorage.setItem("X-token",  res.result.token)
                console.log("X-token",  res)
            }, 1000);
        } else {
            message.style.color = "red";
            message.textContent = res.message || "登录失败，请重试！";
        }

    } catch (error) {
        console.error(error);
        message.style.color = "red";
        message.textContent = "网络错误，请检查连接！";
    }
});